package com.artqiyi.dollmachine.views.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;
import com.artqiyi.dollmachine.R;
import com.artqiyi.dollmachine.common.Base.BaseActivity;
import com.artqiyi.dollmachine.common.constant.Constant;
import com.artqiyi.dollmachine.model.entity.GameRecordBean;
import com.artqiyi.dollmachine.presenter.GameRecordsListPresenter;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.views.adapter.GameRecordsListAdapter;
import com.artqiyi.dollmachine.views.widget.CustomGifHeader;
import com.artqiyi.dollmachine.views.widget.ToolBarView;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * author: PengCheng
 * time: 2017/12/8 0008
 * desc: 最近抓中的记录
 */

public class GameRecordsListActivity extends BaseActivity<GameRecordsListPresenter> implements PresenterCallbackCollection.GameRecordsListActivity {

    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.xRefreshView)
    XRefreshView mXRefreshView;

    private GameRecordsListAdapter mAdapter;

    private List<GameRecordBean> mDollBeans = new ArrayList<>();

    private int pageNum = 1;

    private int machineId;

    @Override
    public GameRecordsListPresenter getPresenter() {
        return new GameRecordsListPresenter();
    }

    @Override
    protected void handleIntent(Intent intent) {
        super.handleIntent(intent);
        machineId = getIntent().getIntExtra(Constant.KEY_MACHINE_ID, 0);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_game_records_list;
    }

    @Override
    protected void handleToolBar(ToolBarView toolBar) {
        super.handleToolBar(toolBar);
        toolBar.setTitleText("最近抓取的记录");
        toolBar.setBackPressed(this);
    }

    @Override
    public void reLoadData() {
        super.reLoadData();
        mPresenter.getMineCatchRecord(machineId, 1);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mAdapter = new GameRecordsListAdapter(mDollBeans);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(GameRecordsListActivity.this));
        mRecyclerView.setAdapter(mAdapter);
        CustomGifHeader header = new CustomGifHeader(this);
        mXRefreshView.setCustomHeaderView(header);
        mXRefreshView.setAutoLoadMore(true);
        mXRefreshView.setPinnedTime(1000);
        mXRefreshView.setMoveForHorizontal(true);
        mXRefreshView.setPullLoadEnable(true);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                pageNum = 1;
                mPresenter.getMineCatchRecord(machineId, pageNum);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                mPresenter.getMineCatchRecord(machineId, pageNum);
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }


    @Override
    public void onGetGameRecordsListSuccess(List<GameRecordBean> beans) {
        mXRefreshView.stopRefresh();
        mXRefreshView.stopLoadMore();

        if (null != beans && beans.size() != 0) {
            if (pageNum == 1) {
                mDollBeans.clear();
            }
            mDollBeans.addAll(beans);
            mAdapter.notifyDataSetChanged();
            if (beans.size() < 20) {
                mXRefreshView.setLoadComplete(true);
            }
            pageNum++;
        }
    }

    @Override
    public void onGetGameRecordsListFail(String msg) {
        mXRefreshView.stopRefresh();
        mXRefreshView.setLoadComplete(true);
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
